package com.ea.common.utils;

/**
 * 正则验证工具
 */
public class RegularUtils {
    private RegularUtils() {}

    //各种正则pattern
    public final static String NICKNAME_REGEX = "^\\S{1,12}$";
    public final static String PASSWORD_REGEX = "^[a-zA-Z0-9_.]{8,20}$";
    public final static String EMAIL_REGEX = "^(([^<>()\\[\\].,;:\\s@\"]+(\\.[^<>()\\[\\].,;:\\s@\"]+)*)|(\".+\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$";
    public final static String PHONE_NUMBER_REGEX = "^(\\+86)?1(3\\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\\d|9[0-35-9])\\d{8}$";

    /**
     * 验证text是否为电子邮箱格式
     * @param text text
     * @return boolean
     */
    public static boolean checkIsEmail(String text) {
        String pattern = EMAIL_REGEX;
        return text.matches(pattern);
    }

    /**
     * 验证text是否为电话号码格式
     * @param text
     * @return
     */
    public static boolean checkIsPhoneNumber(String text) {
        String pattern = PHONE_NUMBER_REGEX;
        return text.matches(pattern);
    }

    /**
     * 验证text是否为密码格式
     * @param text
     * @return
     */
    public static boolean checkIsPassword(String text) {
        String pattern = PASSWORD_REGEX;
        return text.matches(pattern);
    }

    /**
     * 验证text是否为昵称格式
     * @param text
     * @return
     */
    public static boolean checkIsNickname(String text) {
        String pattern = NICKNAME_REGEX;
        return text.matches(pattern);
    }
}
